from pygame.sprite import Sprite, Rect
from pygame.font import Font
from pygame.locals import *
from pygame.mixer import Sound
from pygame import Surface
import actions, scenes
f = Font('Ubuntu-B.ttf',10)
size = [40,20]

grades = 8
period = 5
bonus = Sound("sounds/powerup.ogg")
class Bonus(Sprite):
    
    def __init__(self, color, text, pos):
        Sprite.__init__(self)
        self.images = []
        t = f.render(text,1,(0,0,0))
        t2 = f.render(text,1,(255,255,255))
        for i in reversed(range(grades)):
            s = Surface(size, SRCALPHA)
            s.fill([int(float(c)/grades*(i+1)) for c in color])
            s.blit(t if i%2 else t2,(0,0))
            self.images.append(s)
        self.rect = Rect(0,0,*size).move(pos)
        self.imn = 0
        self.speed = 0.2
        self.update()
        
    def update(self):
        if self.rect.y > scenes.scene.size[1]:
            self.kill()
        self.imn = (self.imn + 1)% len(self.images*period)
        self.image = self.images[self.imn/period]
        self.rect.y += int(self.speed)
        self.speed += 0.5
        
    def pickup(self, paddle):
        bonus.play()
        self.kill()
        self.action(paddle)
        
def slow(pos):
    s = Bonus((0,255,0),"SLOW", pos)
    s.action = actions.slow
    return s

def split(pos):
    s = Bonus((255,0,255),"SPLIT", pos)
    s.action = actions.split
    return s

def large(pos):
    s = Bonus((0,0,255),"LARGE", pos)
    s.action = actions.large
    return s

bonuses = [slow, split, large]